介紹
前篇寫了些 line bot 與heroku 對應的介紹,那樣的寫法是可以透過 endpoint payload 打查詢資料去撈到主機的訊息 ,雖然也可以透過line 的 notifaction 去做推播通知但是這是要付費的,所以如果要做line通知的話可以使用 notify 這個是沒有雙向的服務單純只是通知所以很適合用在 Puppeteer 的通知或是主機的通知
https://notify-bot.line.me/zh_TW/
申請完後會出現這樣的畫面,然後要去Email那邊認證一下信箱
右上的選單有個個人服務,這邊有個人頁面裡面有個開發人員的設定可以指定哪些line 的朋友可以使用這個服務
指定
接下來接續上兩篇的 hreoku + linebot 這篇要來介紹的是 notify的單向推播通知功能
先透過 spwan 這個打開頁面 ,並把config值帶入打開頁面先註冊line notify 認證
var url = "https://notify-bot.line.me/oauth/authorize?client_id=" + client_id + "&response_type=code&redirect_uri=" + redirect_uri + "&scope=notify&state=" + state + "&response_mode=form_post";
webhook callback的地方設定如下
如果 "getAuthCode" === req.body.state 時候 打入的 payload有包含認證過的就發一個post到line提供的
oauth
request.post({
url: "https://notify-bot.line.me/oauth/token",
formData: formData
},
這邊裝一個 readline 可以 在console底下 繼續發送 request post 給 notify去給他推播
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
讓console 輸入資料 rl.question('say words? ', (answer)...
request.post({
url: "https://notify-api.line.me/api/notify",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Bearer " + access_token
},
formData: {
message: answer,
}
import { KeyData } from '../keyData/key'
var client_id = KeyData.client_id;
var client_secret = KeyData.client_secret;
var redirect_uri = KeyData.redirect_uri;
var spawn = require('child_process').spawn
import https = require("https");
import express = require("express");
import bodyParser = require('body-parser');
import request = require("request");
var readline = require("readline");
//1.start to get auth code
var state = "getAuthCode"
var url = "https://notify-bot.line.me/oauth/authorize?client_id=" + client_id + "&response_type=code&redirect_uri=" + redirect_uri + "&scope=notify&state=" + state + "&response_mode=form_post";
console.log(url)
spawn('open', [url]);
let app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.post('/callback', function (req, rep) {
let code = req.body.code;
let uid = req.query.uid;
if ("getAuthCode" === req.body.state) {
//2.start get auth token
var formData = {
grant_type: "authorization_code",
code: code,
redirect_uri: redirect_uri,
client_id: client_id,
client_secret: client_secret
}
request.post({
url: "https://notify-bot.line.me/oauth/token",
formData: formData
},
(err, httpResponse, body) => {
var json = JSON.parse(body);
const access_token = json.access_token;
//set uid with access_token
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let ansyncQ = () => {
rl.question('say words? ', (answer) => {
//3.start to notify
console.log(answer, access_token)
request.post({
url: "https://notify-api.line.me/api/notify",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Bearer " + access_token
},
formData: {
message: answer,
}
},
(err, httpResponse, body) => {
console.log(body);
}
)
ansyncQ();
// rl.close();
});
}
ansyncQ();
});
}
rep.end("ok")
})
app.listen(3000);
總結
nofify 設定上比較多些,但是因為 puppeteer 用這個個人覺得不用錢的可以做到line通知也是很不錯,附上程式碼
github.com/polo13999/linenotify
原始碼參考程式 特別感謝 wolke lin 大大 教學
https://github.com/Wolke/line_notify_example